package com.dtda.edu.miaosha.controller;
/**
 * @author 袁强
 * @version 2019年2月19日 上午11:05:43
 * 类说明  
 */


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.bo.UserInfoBo;
import com.dtda.edu.miaosha.common.exception.BaseExcpController;
import com.dtda.edu.miaosha.common.response.ResponseType;
import com.dtda.edu.miaosha.service.ItemService;
import com.dtda.edu.miaosha.vo.ItemVo;


@RestController
@RequestMapping("/")
public class ItemController  extends BaseExcpController{

	@Autowired
	private ItemService itemService;
	
	/**
	 * 节点:添加商品
	 * @param title
	 * @param price
	 * @param description
	 * @param imgUrl
	 * @param titleStock
	 * @return
	 * @throws BusinessException
	 */
	/**
	 * 节点:商品详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getItem")
	@ResponseBody
	public ResponseType getItem(@RequestParam(name="id")Integer id){
		ItemBo itemBo = itemService.showItemById(id);
		if(itemBo == null) {
			return ResponseType.create("fail", "商品不存在!");
		}
		ItemVo itemVo = new ItemVo();
		BeanUtils.copyProperties(itemBo, itemVo);
		if(itemBo.getMarketBo()!=null) {
			//商品还有秒杀活动(未开始或正在进行) 0未开始  1正在进行 2已结束/没有活动
			itemVo.setMarketStatus(itemBo.getMarketBo().getMarketStatus());
			itemVo.setMarketId(itemBo.getMarketBo().getId());
			itemVo.setMarketPrice(itemBo.getMarketBo().getMarketItemPrice());
			itemVo.setMarketStartDate(itemBo.getMarketBo().getStartDate());
		}else {
			itemVo.setMarketStatus(2);
		}
		return ResponseType.create(itemVo);
	}
	/**
	 * 节点:所有商品
	 * 
	 */
	@RequestMapping("/showItems")
	@ResponseBody
	public ResponseType showItems(){
		List<ItemBo> items = itemService.showItems();
		if(items.size() == 0) {
			return ResponseType.create("fail", "没有活动商品!");
		}
		return ResponseType.create(items);
	}
	@RequestMapping("/goShowItems")
	public ModelAndView goShowItems(HttpServletRequest request) {
		 ModelAndView modelAndView = new ModelAndView();
		 UserInfoBo userInfoBo = (UserInfoBo) request.getSession().getAttribute("LOGIN_USER");
		 modelAndView.addObject("userName",userInfoBo);
		 modelAndView.setViewName("/project/showItems");
		 return modelAndView;
		 }
	@RequestMapping("/goItemDetail")
	@ResponseBody
	public ModelAndView goItemDetail(@RequestParam(name="id")String id) {
		 ModelAndView modelAndView = new ModelAndView();
		 modelAndView.addObject("itemId", id);
		 modelAndView.setViewName("/project/ItemDetail");
		 return modelAndView;
	}
}
